import {
  getHeroListService,
  getHeroInfoService,
  createHeroService,
  updateHeroService,
  deleteHeroService,
} from "../service/hero.service.js";
import sequelize from "../db/seq.js";
import {
  createSkillService,
  updateSkillService,
} from "../service/skill.service.js";

// 获取所有英雄
export const getHeroList = async (ctx, next) => {
  const { name, classification, position } = ctx.request.query;
  const res = await getHeroListService({ name, classification, position });
  ctx.body = {
    code: 200,
    message: "获取英雄列表成功",
    result: res,
  };

  await next();
};
// 根据id获取英雄信息
export const getHeroById = async (ctx, next) => {
  const { id } = ctx.request.query;

  try {
    const res = await getHeroInfoService(id);

    ctx.body = {
      code: 200,
      message: "获取英雄信息成功",
      data: res,
    };
  } catch (err) {
    return ctx.app.emit(
      "error",
      {
        code: 10001,
        message: "查询英雄信息失败",
        result: "",
      },
      ctx
    );
  }

  await next();

  // console.log(id);
  // const res = await getHeroInfoService(id);
  // console.log("res============", res);
  // ctx.body = {
  //   code: 200,
  //   message: "获取英雄信息成功",
  //   data: res,
  // };
};
//新增英雄
export const createdHero = async (ctx, next) => {
  const { name, price, story, classification, position, skin, skillList } =
    ctx.request.body;
  try {
    // 开启事务确保数据唯一性
    const transaction = await sequelize.transaction();
    // 创建英雄
    try {
      const newHero = await createHeroService(
        {
          name,
          price,
          story,
          classification,
          position,
          skin,
        },
        {
          transaction,
        }
      );
      //批量创建技能并关联到英雄
      const newSkillList = skillList.map((skill) => {
        return {
          ...skill,
          heroId: newHero.heroId,
        };
      });
      const res = await createSkillService(newSkillList, {
        transaction,
      });
      // 提交事务
      await transaction.commit();
      ctx.body = {
        code: 200,
        message: "创建英雄成功",
        data: newHero,
        skills: res,
      };
    } catch (error) {
      await transaction.rollback(); // 失败了就回滚
      return ctx.app.emit(
        "error",
        {
          code: 10001,
          message: "创建失败",
          result: "",
        },
        ctx
      );
    }

    //返回包含技能的英雄数据
  } catch (error) {
    return ctx.app.emit(
      "error",
      {
        code: 10001,
        message: "创建失败,已存在",
        result: error,
      },
      ctx
    );
  }

  await next();
};
// 修改英雄信息
export const updateHero = async (ctx, next) => {
  const id = ctx.params.id;
  const { name, price, story, classification, position, skin, skillList } =
    ctx.request.body;
  const transaction = await sequelize.transaction();
  try {
    // 1. 更新英雄信息
    await updateHeroService(
      id,
      {
        name,
        price,
        story,
        classification,
        position,
        skin,
      },
      {
        transaction,
      }
    );

    // 2. 更新技能信息（假设skillData包含技能ID和描述）
    for (const skill of skillList) {
      await updateSkillService(skill.skillId, skill, {
        transaction,
      });
    }

    // 提交事务
    await transaction.commit();
    ctx.body = {
      code: 200,
      message: "更新英雄信息成功",
      data: null,
    };
  } catch (error) {
    console.log("errorerrorerror", error);
    // 事务回滚
    await transaction.rollback();
    return ctx.app.emit(
      "error",
      {
        code: 10001,
        message: "更新失败",
        result: "",
      },
      ctx
    );
  }

  await next();
};
// 删除英雄
export const deleteHero = async (ctx, next) => {
  const { id } = ctx.params;

  try {
    const res = await deleteHeroService(id);
    if (res) {
      ctx.body = {
        code: 200,
        message: "删除成功",
        result: res,
      };
    } else {
      ctx.body = {
        code: 10001,
        message: "删除失败",
        result: "",
      };
    }
  } catch (error) {
    return ctx.app.emit(
      "error",
      {
        code: 10001,
        message: "删除失败",
        result: "",
      },
      ctx
    );
  }

  await next();
};
